#!/bin/rc

awk -v 'objtype='$objtype -v 'pwd='`{pwd} '
BEGIN{
	if(ARGC < 2)
		exit
}

/^[ \t]*$/{
	next;
}
/^#/{
	next;
}
collect && /^[^ \t]/{
	collect = 0;
}
collect && section == "dev"{
	dev[ndev++] = $1;
	if($1 == "ad")
		devad = 1;
	else if($1 == "sd")
		devsd = 1;
	else if($1 == "uart")
		devuart = 1;
	else if($1 == "vga")
		devvga = 1;
	for(i = 2; i <= NF; i++){
		if($i == "dma" && objtype ~ "(386|alpha|amd64)")
			i8237dma++;
	}
}
collect && section == "ip"{
	ip[nip++] = $1;
}
collect && section == "link"{
	link[nlink++] = $1;
}
collect && section == "misc"{
	sub(/^[ \t]*/, "");
	misc[nmisc++] = $1;
	if($1 ~ "^arch.*")
		arch[narch++] = $1;
	else if($1 ~ "^ad.*")
		adifc[nadifc++] = $1;
	else if($1 ~ "^sd.*")
		sdifc[nsdifc++] = $1;
	else if($1 ~ "^uart.*")
		physuart[nphysuart++] = substr($1, 5, length($1)-4) "physuart";
	else if($1 ~ "^vga.*"){
		if(NF == 1)
			vgadev[nvgadev++] = $1;
		else for(i = 2; i <= NF; i++){
			if($i ~ "[+]cur")
				vgadev[nvgadev++] = $1;
			if($i ~ "[+=]cur")
				vgacur[nvgacur++] = $1;
		}
	}
}

collect && section == "port"{
	sub(/^[ \t]*/, "");
	port[nport++] = $0;
}
$0 ~ /^[^ \t]/{
	if($1 ~ "(bootdir|dev|ip|link|misc|port)"){
		section = $1;
		collect = 1;
	}
	next;
}

END{
	if(ARGC < 2)
		exit "usage"

	printf "#include \"u.h\"\n";
	printf "#include \"../port/lib.h\"\n";
	printf "#include \"mem.h\"\n";
	printf "#include \"dat.h\"\n";
	printf "#include \"fns.h\"\n";
	printf "#include \"io.h\"\n";
	printf "#include \"../port/error.h\"\n\n";

	for(i = 0; i < ndev; i++)
		printf "extern Dev %sdevtab;\n", dev[i];
	printf "Dev* devtab[]={\n";
	for(i = 0; i < ndev; i++)
		printf "\t&%sdevtab,\n", dev[i];
	printf "\tnil,\n};\n\n";

	if(objtype ~ "(386|alpha|amd64)"){
		alloc = "nil";
		if(i8237dma){
			printf "extern void _i8237alloc(void);\n";
			alloc = "_i8237alloc";
		}
		printf "void (*i8237alloc)(void) = %s;\n", alloc;
		printf "int i8237dma = %d;\n\n", i8237dma;
	}

	for(i = 0; i < nlink; i++)
		printf "extern void %slink(void);\n", link[i];

	printf "void links(void){\n";
	print "\tbootlinks();\n";
	for(i = 0; i < nlink; i++)
		printf "\t%slink();\n", link[i];
	printf "}\n\n";

	if(narch || objtype == "386"){
		for(i = 0; i < narch; i++)
			printf "extern PCArch %s;\n", arch[i];
		printf "PCArch* knownarch[] = {\n";
		for(i = 0; i < narch; i++)
			printf "\t&%s,\n", arch[i];
		printf "\tnil,\n};\n\n";
	}

	if(devad){
		printf "#include \"../port/ad.h\"\n";
		for(i = 0; i < nadifc; i++)
			printf "extern ADifc %sifc;\n", adifc[i];
		printf "ADifc* adifc[] = {\n";
	  	for(i = 0; i < nadifc; i++)
			printf "\t&%sifc,\n", adifc[i];
	  	printf "\tnil,\n};\n\n";
	}
	if(devsd){
		printf "#include \"../port/sd.h\"\n";
		for(i = 0; i < nsdifc; i++)
			printf "extern SDifc %sifc;\n", sdifc[i];
		printf "SDifc* sdifc[] = {\n";
	  	for(i = 0; i < nsdifc; i++)
			printf "\t&%sifc,\n", sdifc[i];
	  	printf "\tnil,\n};\n\n";
	}

	if(devuart){
		for(i = 0; i < nphysuart; i++)
			printf "extern PhysUart %s;\n", physuart[i];
		printf "PhysUart* physuart[] = {\n";
	  	for(i = 0; i < nphysuart; i++)
			printf "\t&%s,\n", physuart[i];
	  	printf "\tnil,\n};\n\n";
	}

	if(devvga){
		printf "#define	Image	IMAGE\n";
		printf "#include <draw.h>\n";
		printf "#include <memdraw.h>\n";
		printf "#include <cursor.h>\n";
		printf "#include \"screen.h\"\n";

		for(i = 0; i < nvgadev; i++)
			printf "extern VGAdev %sdev;\n", vgadev[i];
	  	printf "VGAdev* vgadev[] = {\n";
	  	for(i = 0; i < nvgadev; i++)
			printf "\t&%sdev,\n", vgadev[i];
	  	printf "\tnil,\n};\n\n";

		for(i = 0; i < nvgacur; i++)
			printf "extern VGAcur %scur;\n", vgacur[i];
	  	printf "VGAcur* vgacur[] = {\n";
	  	for(i = 0; i < nvgacur; i++)
			printf "\t&%scur,\n", vgacur[i];
	  	printf "\tnil,\n};\n\n";
	}
	
	if(nip){
		printf "#include \"../ip/ip.h\"\n";
		for(i = 0; i < nip; i++)
			printf "extern void %sinit(Fs*);\n", ip[i];
		printf "void (*ipprotoinit[])(Fs*) = {\n";
		for(i = 0; i < nip; i++)
			printf "\t%sinit,\n", ip[i];
		printf "\tnil,\n};\n\n";
	}

	if(nport){
		for(i = 0; i < nport; i++)
			printf "%s\n", port[i];
		printf "\n";
	}

	printf "char* conffile = \"%s/%s\";\n", pwd, ARGV[1];
	printf "ulong kerndate = KERNDATE;\n";

	exit
}' $*
